package jt.classic.app.hys.hyszy;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.user.IUser;

import org.limp.basework.console.XSystemConsole;
import org.limp.basework.console.XSystemConsoleRunException;
import org.limp.basework.impl.CommonBean4HSQ;

import org.limp.mine.Controller;
import org.limp.mine.Label;
import org.limp.mine.QueryJack;
import org.limp.mine.QueryJackFactory;
import org.limp.mine.StringTool;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.sql.Connection;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class HyszyAction extends SimpleAction {
    /**
     * 新建用户
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward neu(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        return mapping.findForward("neu");
    }

    /**
     * 保存模块定义
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward save(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            XSystemConsole console = new XSystemConsole();
            console.RUN2SAVE(request, conn);
            conn.commit();

            String url = "/hyszy.do?method=list";

            forward = new ActionForward();
            forward.setPath(url);
            forward.setRedirect(true);
        } catch (XSystemConsoleRunException ex) {
            conn.rollback();
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 编辑用户信息
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            HyszyManager manager = new HyszyManager(conn);

            Map hyszy = manager.gethyszyById(id);

            request.setAttribute("hyszy", hyszy);

            forward = mapping.findForward("edit");
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 编辑用户信息
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward view(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            HyszyManager manager = new HyszyManager(conn);

            Map hyszy = manager.gethyszyById(id);

            request.setAttribute("hyszy", hyszy);

            forward = mapping.findForward("view");
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 修改用户信息
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward update(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;
        String id = StringTool.checkString(request.getParameter("HYS_ID"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            XSystemConsole console = new XSystemConsole();
            console.RUN2UPDATE(request, conn);

            conn.commit();

            String url = "/hyszy.do?method=edit&id=" + id;

            forward = new ActionForward();
            forward.setPath(url);
            forward.setRedirect(true);
        } catch (XSystemConsoleRunException ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 会议室资源管理列表
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String hname = StringTool.checkString(param.get("hname"));

        request.setAttribute("hname", hname);

        String hgsname = StringTool.checkString(param.get("hgsname"));

        request.setAttribute("hgsname", hgsname);

        IUser user = ISystem.getSessionUser(request);

        StringBuffer where = new StringBuffer(" WHERE 1=1 ");

        if (ISystem.isAdmin(user)) {
            //donothing
        } else if (ISystem.matchrole(user, "hysadmin")) {
            if (user.igroup() != null) {
                String gid = user.igroup().ibranchgroup(0);

                where.append(" AND HYS_GSID_NAME = '" + gid + "' ");
            }
        } else {
            where.append(" AND CREATE_USER = '" + user.id() + "' ");
        }

        if (!hname.equals("")) {
            where.append(" AND HYS_MC LIKE '%" + hname + "%' ");
        }

        if (!hgsname.equals("")) {
            where.append(" AND HYS_GSID_NAME LIKE '%" + hgsname + "%' ");
        }

        String sql1 = "SELECT COUNT(HYS_ID) FROM APP_H_HYSZY " +
            where.toString();
        String sql2 = "SELECT * FROM APP_H_HYSZY " + where.toString() +
            " ORDER BY HYS_ID ASC";

        String url = request.getContextPath() + "/app/hyszy.do?method=list";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

            // 设置
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, "20");
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List resultList = plus.work();

            /**
            if (!resultList.isEmpty()) {
                ModuleManager manager = new ModuleManager(conn);

                for (int i = 0; i < resultList.size(); i++) {
                    String id = (String) ((Map) resultList.get(i)).get("ID");
                    IModule module = manager.getModuleById(id);
                    ((Map) resultList.get(i)).put("module", module);
                }
            }
                        */

            // 获得跳转控制
            Controller controller = plus.getController("form1");

            // 设置调转类型
            controller.setType("input");

            // 显示标签
            Label label = plus.getLabel();

            // 设置显示标签类型
            label.setType("href");

            Object[] result = new Object[4];
            result[0] = resultList;
            result[1] = label;
            result[2] = controller;
            result[3] = pageNO;

            request.setAttribute("result", result);
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("list");
    }

    /**
     * 会议室资源查看列表
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward list4query(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String hname = StringTool.checkString(param.get("hname"));

        request.setAttribute("hname", hname);

        String hgsname = StringTool.checkString(param.get("hgsname"));

        request.setAttribute("hgsname", hgsname);

        IUser user = ISystem.getSessionUser(request);

        StringBuffer where = new StringBuffer(" WHERE 1=1 ");

        if (ISystem.isAdmin(user)) {
            //donothing
        } else {
            if (user.igroup() != null) {
                String gname = user.igroup().ibranchgroup(0);
                where.append(" AND HYS_GSID_NAME = '" + gname + "' ");
            }
        }

        if (!hname.equals("")) {
            where.append(" AND HYS_MC LIKE '%" + hname + "%' ");
        }

        if (!hgsname.equals("")) {
            where.append(" AND HYS_GSID_NAME LIKE '%" + hgsname + "%' ");
        }

        String sql1 = "SELECT COUNT(HYS_ID) FROM APP_H_HYSZY " +
            where.toString();
        String sql2 = "SELECT * FROM APP_H_HYSZY " + where.toString() +
            " ORDER BY HYS_ID ASC";

        String url = request.getContextPath() + "/app/hyszy.do?method=list4query";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

            // 设置
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, "20");
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List resultList = plus.work();

            /**
            if (!resultList.isEmpty()) {
                ModuleManager manager = new ModuleManager(conn);

                for (int i = 0; i < resultList.size(); i++) {
                    String id = (String) ((Map) resultList.get(i)).get("ID");
                    IModule module = manager.getModuleById(id);
                    ((Map) resultList.get(i)).put("module", module);
                }
            }
                        */

            // 获得跳转控制
            Controller controller = plus.getController("form1");

            // 设置调转类型
            controller.setType("input");

            // 显示标签
            Label label = plus.getLabel();

            // 设置显示标签类型
            label.setType("href");

            Object[] result = new Object[4];
            result[0] = resultList;
            result[1] = label;
            result[2] = controller;
            result[3] = pageNO;

            request.setAttribute("result", result);
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("list4query");
    }

    /**
     * 会议室资源查看
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward query(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String pageNO = StringTool.checkString(param.get("pageNO"));

        IUser user = ISystem.getSessionUser(request);

        StringBuffer where = new StringBuffer(" WHERE 1=1 ");

        if (ISystem.isAdmin(user)) {
            //donothing
        } else if (ISystem.matchrole(user, "hysadmin")) {
            if (user.igroup() != null) {
                String gname = user.igroup().ibranchgroup(0);
                where.append(" AND HYS_GSID_NAME = '" + gname + "' ");
            } else {
                where.append(" AND HYS_GSID_NAME = 'noname' ");
            }
        } else {
            where.append(" AND CREATE_USER = " + user.id() + " ");
        }

        String sql1 = "SELECT COUNT(HYS_ID) FROM APP_H_HYSZY " +
            where.toString();
        String sql2 = "SELECT * FROM APP_H_HYSZY " + where.toString() +
            " ORDER BY HYS_ID ASC";

        String url = request.getContextPath() + "/app/hyszy.do?method=query";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

            // 设置
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, "20");
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List resultList = plus.work();

            /**
            if (!resultList.isEmpty()) {
                ModuleManager manager = new ModuleManager(conn);

                for (int i = 0; i < resultList.size(); i++) {
                    String id = (String) ((Map) resultList.get(i)).get("ID");
                    IModule module = manager.getModuleById(id);
                    ((Map) resultList.get(i)).put("module", module);
                }
            }
                        */

            // 获得跳转控制
            Controller controller = plus.getController("form1");

            // 设置调转类型
            controller.setType("input");

            // 显示标签
            Label label = plus.getLabel();

            // 设置显示标签类型
            label.setType("href");

            Object[] result = new Object[4];
            result[0] = resultList;
            result[1] = label;
            result[2] = controller;
            result[3] = pageNO;

            request.setAttribute("result", result);
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("query");
    }

    /**
     * 删除用户（物理删除）
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        String ids = StringTool.checkString(request.getParameter("ids"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            HyszyManager manager = new HyszyManager(conn);

            manager.deleteS(ids);

            conn.commit();

            forward = mapping.findForward("delete");
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }
}
